/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.model.newmodel.LisCharginitem;
import com.cxhd.lis.model.newmodel.LisCharginitemTest;
import static com.cxhd.lis.model.newmodel.LisCharginitemTest.lisCharginitemTestdao;
import com.cxhd.lis.model.newmodel.LisSampletype;
import com.cxhd.lis.model.newmodel.LisTestdescribe;
import com.cxhd.lis.model.newmodel.PpDeviceTest;
import static com.cxhd.lis.model.newmodel.PpDeviceTest.ppDeviceTestdao;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cxhd
 */
@ControllerBind(controllerKey = "/view/lis/citemtest")
public class LisCharginitemTestCtrl extends Controller{
    Gson  gson=new Gson();
     private LisTestdescribe test=new LisTestdescribe();
    private LisSampletype sam=new LisSampletype();
    LisCharginitem citem=new LisCharginitem();
     public void index(){
        render("/view/lis/sfxmpp.jsp");
    }
       /**
      * 收费项目显示
      */
    public void getLisCharginitem(){
        String sql="select PK ITEMPK,ITEMID,ITEMNAME,PRICE,DATA1,DATA2,DATA3 ";
        String param=" from lis_charginitem ";
        //"where ITEMNAME LIKE '%"+getPara("searchContent")+"%'";
        Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage",getParaToInt("currentPage")), getParaToInt("pageSize",getParaToInt("pageSize")), sql, param);
renderJson(userPage);
//        renderJson(Db.use("mysql").find("select PK ITEMPK,ITEMID,ITEMNAME,PRICE,DATA1,DATA2,DATA3  from lis_charginitem "));
    }
    /**
     * 项目显示
     */
    public void getLisTest(){ 
//        return Db.use("mysql").find("select t.PK TESTPK,t.PRICE, t.ENGLISHNAME,t.CHINESENAME,t.TESTID ,t.SAMPLETYPE,\n" +
//"(SELECT s.SAMPLEDESCRIBE  FROM lis_sampletype s WHERE s.SAMPLETYPE=t.SAMPLETYPE) SAMPLEDESCRIBE \n" +
// " from lis_testdescribe t WHERE  t.PK=? ",PK);    
//       renderJson( test.getLisTests(getPara("PK")));
String sql="select t.PK TESTPK,t.PRICE, t.ENGLISHNAME,t.CHINESENAME,t.TESTID ,t.SAMPLETYPE,(SELECT s.SAMPLEDESCRIBE  FROM lis_sampletype s WHERE s.PK=t.SAMPLETYPE) SAMPLEDESCRIBE ";
String param="from lis_testdescribe t WHERE  t.SAMPLETYPE="+Integer.valueOf(getPara("PK"));
Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage",getParaToInt("currentPage")), getParaToInt("pageSize",getParaToInt("pageSize")), sql, param);
renderJson(userPage);
    }
    public void getLisTestByPy(){//通过项目代码查询项目
//        renderJson(test.getLisTestByPy(getPara("PY")));
String sql="select t.PRICE, t.ENGLISHNAME,t.CHINESENAME,t.TESTID,t.PK TESTPK,t.SAMPLETYPE,(SELECT s.SAMPLEDESCRIBE  FROM lis_sampletype s WHERE s.PK=t.SAMPLETYPE) SAMPLEDESCRIBE";
     String param=" from lis_testdescribe t WHERE  t.TESTID like '%"+getPara("TESTID")+"%'";
     if(getPara("PK")!=""&&getPara("PK")!=null){
         param=" from lis_testdescribe t WHERE  t.TESTID like '%"+getPara("TESTID")+"%' and t.SAMPLETYPE="+Integer.valueOf(getPara("PK"));
     }
Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage",getParaToInt("currentPage")), getParaToInt("pageSize",getParaToInt("pageSize")), sql, param);
renderJson(userPage);
        
    }
    /**
     * 获取标本类型  ---用于筛选项目
     */
    public void getLisSam(){
        renderJson(sam.getLisSams());
    }

    /**
     * 显示选中的匹配
     */
    public void getLisCharginitemTest(){
        renderJson(Db.use("mysql").find("select PK, (SELECT  c.ITEMNAME FROM lis_charginitem c where c.ITEMID=ct.ITEMID ) ITEMID1,\n" +
"(SELECT t.CHINESENAME FROM lis_testdescribe t WHERE t.TESTID=ct.TESTID ) TESTID1,ct.ITEMID,ct.TESTID \n" +
"FROM lis_charginitem_test ct where ct.ITEMID=?",getPara("ITEMID")));
    }
    /**
     * 保存仪器与项目的匹配
     */
    @Before(Tx.class)
    public void saveLisCharginitemTest(){
        List<Map<String, String>> tests = gson.fromJson(getPara("TESTS"), new TypeToken<List<Map<String, String>>>() {}.getType());
        //查询该收费项目是否匹配相关样品类型的项目
//        List<Record> list=Db.use("mysql").find("select * from lis_charginitem_test where DATA1=? and ITEMID=? limit 0,10",tests.get(0).get("SAMPLETYPE"),getPara("ITEMID"));
        //删除改收费项目相关样品类型的所有项目匹配
//        if(list.size()>0){
//        lisCharginitemTestdao.delectCitemTests(tests.get(0).get("SAMPLETYPE"), getPara("ITEMID"));
//        }
        //为该收费项目添加项目
        double sum=0;
        for(Map<String,String> tmp : tests){
            List<Record> list=Db.use("mysql").find("select * from lis_charginitem_test where ITEMID=? AND TESTID=?",getPara("ITEMID"),tmp.get("TESTID"));
            if(list.size()==0){
            new LisCharginitemTest().saveCitemTest(getPara("ITEMID"), tmp.get("TESTID"),tmp.get("SAMPLETYPE"),tmp.get("TESTPK"),getPara("ITEMPK"));
             //修改收费项目的金额
             String pri=tmp.get("PRICE");
             if(pri==""||pri==null){
                 pri="0";
             }
             sum=sum+Double.valueOf(pri);
            }
            
        }
        System.out.println(getPara("ITEMPK"));
        System.out.println(sum+"");
        citem.updatePrice(getPara("ITEMPK"), sum+"");
        renderText("true");
    }
    /**
     * 注意删除需要一些必要的判断 该匹配是否被使用
     * 待完成 删除收费项目与项目的匹配
     */
    @Before(Tx.class)
    public void deleteLisCharginitemTest(){
        lisCharginitemTestdao.delectCitemTest(getPara("TESTID"), getPara("ITEMID"));
        //修改收费项目的金额
        
      List<Record>  re=  Db.use("mysql").find("select SUM(price) sum from lis_testdescribe t RIGHT JOIN  lis_charginitem_test ct on t.TESTID=ct.TESTID   WHERE ct.ITEMID= ? and ct.TESTID!=?",getPara("ITEMID"),getPara("TESTID"));
      String sum="0";
      if(re.get(0).get("sum")!=null){
         sum= re.get(0).getBigDecimal("sum").toString();
      }
      Record  re2=Db.use("mysql").findFirst("select PK from lis_charginitem where ITEMID=?",getPara("ITEMID"));
      citem.updatePrice(re2.getInt("PK").toString(), sum);
        renderText("true");
    }
    
//    public void updateLisCharginitemTest(){
//        ppDeviceTestdao.updatePpDeviceTest(getPara("PK"),getPara("TESTID"),getPara("DEVICEID") ,getPara("DATA1"));
//         renderText("true");
//    }
    public void updateTestpk(){//修改收费项目匹配 收费项目PK 和项目PK
        List<Record> list=Db.use("mysql").find("select ct.PK,ct.TESTID,ct.ITEMID,t.PK PK2,c.PK PK3   from lis_charginitem_test ct inner join lis_testdescribe  t on ct.TESTID=t.TESTID INNER JOIN lis_charginitem c on ct.ITEMID=c.ITEMID");
        for(Record tmp:list){
              Db.update("update lis_charginitem_test set TESTPK='"+tmp.getInt("PK2")+"' , ITEMPK='"+tmp.getInt("PK3")+"' WHERE TESTID='"+tmp.getStr("TESTID")+"' AND ITEMID ='"+tmp.getInt("ITEMID")+"'  ");
          
         // lisCharginitemTestdao.updateTestpk(tmp.getInt("ITEMID"),tmp.getInt("PK"),tmp.getStr("TESTID"),tmp.getInt("PK2"),tmp.getInt("PK3"));
        }
        renderText("true");
    }
  
}
